<?php defined('SYSPATH') or die('No direct access allowed.');

class Controller_CPanel_Auth extends Controller_CPanel {

    public $template = "cpanel/auth";
    public $required_role = null;

    public function action_index() {
        /*
        $user = ORM::factory('user');
        $user->values(array(
            'username' => '',
            'password' => '',
            'email' => ''
        ));
        
        $user->save();
        
        $user->add('roles', ORM::factory('role')->where('name', '=', 'login')->find());
        $user->add('roles', ORM::factory('role')->where('name', '=', 'admin')->find());
        
        $user->save();
        */
        
        if (Auth::instance()->logged_in('admin'))
            Request::current()->redirect(Route::url('cpanel'));
        
        if (Auth::instance()->logged_in('admin'))
            Request::current()->redirect(Route::url('cpanel', array('controller' => 'cpanel')));
        
        if (Auth::instance()->logged_in())
            Request::current()->redirect(Route::url('default'));
        
        if (isset($_POST['username'])) {
            if (Auth::instance()->login(Arr::get($_POST, 'username', ''), Arr::get($_POST, 'password', '')))
                Request::current()->redirect(Route::url('cpanel'));
            else
                $this->add_message('Invalid username or password!', 'error');
        }
    }

    public function action_logout() {
        Auth::instance()->logout();
        Request::current()->redirect(Route::url('cpanel', array('controller' => 'auth')));
    }

}